package com.itheima.factory;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * 用于生成dao接口代理实现类的工厂
 * @author 黑马程序员
 * @Company http://www.itheima.com
 */
public class MapperFactory {

     private static SqlSessionFactory sqlSessionFactory;
     //为每个线程存储数据的
     public static ThreadLocal<SqlSession> tl = new ThreadLocal<SqlSession>();


     static{
         try{
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml"));
         }catch (Exception e){
            e.printStackTrace();
         }
     }


     public static SqlSession getSqlSession(){
         SqlSession sqlSession = sqlSessionFactory.openSession();

         //将sqlSession存到当前访问的线程中
         tl.set(sqlSession);

         return  sqlSession;
     }

}
